package com.game77.guessTheWords2.layer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import android.graphics.Typeface;

import com.game77.guessTheWords2.R;
import com.game77.guessTheWords2.core.GameSystem;
import com.game77.guessTheWords2.scene.MainScene;
import com.game77.guessTheWords2.util.GameUtil;
import com.wiyun.engine.actions.Action;
import com.wiyun.engine.actions.Animate;
import com.wiyun.engine.actions.CallFunc;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.nodes.Animation;
import com.wiyun.engine.nodes.ColorLayer;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.types.WYColor4B;
import com.wiyun.engine.types.WYPoint;

public class LoadingLayer extends ColorLayer {

	public static List<String> gameLoadingTips;

	public LoadingLayer() {
		super(WYColor4B.make(255, 255, 255, 255));

		// 初始化游戏Loading提示信息
		// TODO lc 考虑使用网络动态
		gameLoadingTips = new ArrayList<String>();
		gameLoadingTips.add("输入正确的汉字拼音首字母即可完成题目");
		gameLoadingTips.add("不知道答案怎么办，可以使用提示道具哦");
		gameLoadingTips.add("时刻保持最新版本，可以获得更多题目和功能哦！");

		// 显示loading动画
		Animation animation = new Animation(0, 0.3f, R.drawable.loading_0,
				R.drawable.loading_1, R.drawable.loading_2,
				R.drawable.loading_3, R.drawable.loading_4,
				R.drawable.loading_5, R.drawable.loading_6);
		animation.autoRelease();
		Animate animate = Animate.make(animation);
		animate.autoRelease();

		Sprite sprite = Sprite.make(R.drawable.loading_0);
		addChild(sprite);
		sprite.setPosition(GameSystem.Screen_Width / 2,
				GameSystem.Screen_Height / 2 + 30);
		sprite.autoRelease();
		Sprite logo = Sprite.make(R.drawable.logo77);
		logo.autoRelease();
		logo.setPosition(GameSystem.Screen_Width / 2,
				GameSystem.Screen_Height / 2 + 90);
		addChild(logo);

		sprite.runAction((Action) RepeatForever.make(animate).autoRelease());
		sprite.runAction((CallFunc) CallFunc.make(this, "loadResource")
				.autoRelease());

		Sprite wilogo = Sprite
				.make(Texture2D.makePNG(R.drawable.wiengine_logo));
		addChild(wilogo);
		wilogo.setAnchor(1, 0);
		wilogo.setPosition(WYPoint.make(GameSystem.Screen_Width - 5, 5));
		wilogo.autoRelease();

		// TODO lc 显示游戏Tip
		Random r = new Random((new Date()).getTime());
		Label tip = Label.make(
				gameLoadingTips.get(r.nextInt(gameLoadingTips.size())), 18,
				Typeface.BOLD);
		tip.setColor(WYColor3B.make(0, 0, 0));
		tip.setPosition(GameSystem.Screen_Width / 2,
				GameSystem.Screen_Height / 2 - 60);
		this.addChild(tip);

	}

	/**
	 * 加载系统资源
	 */
	public void loadResource() {
		new Thread() {
			public void run() {
				GameSystem.loadAllResource();
				gotoGameMenu();
			}
		}.start();
	}

	/**
	 * 切换到游戏菜单s
	 */
	public void gotoGameMenu() {
		GameUtil.switchSceneNormal(MainScene.make());
	}
}
